package ranveterinaria.servlet;

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import ranveterinaria.modelo.DoctorModelo;
import ranveterinaria.negocio.GestionDoctor;

/**
 * Servlet implementation class DoctorEditar
 */
@WebServlet("/DoctorEditar")
public class DoctorEditar extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DoctorEditar() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub		

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		try {

			DoctorModelo editar = new DoctorModelo();			
			editar.setCodigoDoctor(Integer.parseInt(request.getParameter("id")));
			editar.setNombreDoctor(request.getParameter("nombre"));
			editar.setApePatDoctor(request.getParameter("apepaterno"));
			editar.setApeMatDoctor(request.getParameter("apematerno"));
			editar.setEmailDoctor(request.getParameter("email"));
			editar.setTlfDoctor(request.getParameter("telefono"));
			
			String[] strchkAdministrador= request.getParameterValues("esadmin");
			if (strchkAdministrador == null){
				editar.setBolAdminDoctor( false );
			}
			else {
				editar.setBolAdminDoctor(true ) ;
			} 
				
			GestionDoctor negocio = new GestionDoctor();
			negocio.actualizar(editar);
			request.setAttribute("MENSAJEEXITO", "El doctor: " + request.getParameter("nombre") +" "+request.getParameter("apepaterno") +" "+ request.getParameter("apematerno")+ " fue actualizado exitosamente");
			RequestDispatcher rd = request.getRequestDispatcher("Formularios/Doctor.jsp");
			rd.forward(request, response);
			
		}catch (ranveterinaria.excepcion.DAOExcepcion ex) {
			request.setAttribute("MENSAJEERROR", ex.getMessage());
			RequestDispatcher rd = request.getRequestDispatcher("Formularios/Doctor.jsp");
			rd.forward(request, response);	
		}catch (Exception e) {
			request.setAttribute("MENSAJEERROR", e.getMessage());
			RequestDispatcher rd = request.getRequestDispatcher("Formularios/Doctor.jsp");
			rd.forward(request, response);			
		}
	}

}
